
Scene = {
	GridWidth = 16,
	GridHeight = 16,
	TileWidth = 16,
	TileHeight = 16,
	MapSize = {x=15,y=15,z=15},
	Bounds = {x=0,y=0,w=graphics.ScrW(),h=graphics.ScrH()},
	sheet = nil,
	Map = {}
}
class(Scene)

function Scene:new()
	self.sheet = sheet
	table.insert(MainState.Renderables, self)
	--self:Resize()
end

function Scene:Resize()
	local s = math.min(self.Bounds.w, self.Bounds.h)
	self.GridWidth = self.Bounds.w/self.MapSize.x
	self.GridHeight = self.Bounds.h/self.MapSize.y
	self.GridWidth = math.min(self.GridWidth,self.GridHeight)
	self.GridHeight = math.min(self.GridWidth,self.GridHeight)
end

function Scene:getTile(x,y)
	return self.Map[x..","..y]
end
function Scene:setTile(value,x,y)
	self.Map[x..","..y] = value
end

function Scene:Update()
	local gx,gy = input.mousePos()
	gx=math.floor(gx/self.GridWidth)
	gy=math.floor(gy/self.GridHeight)

	-- add cell under mouse
	if input.mouseDown(1) then
		self:setTile(RedstoneDust(gx,gy), gx,gy)
	end
	-- delete cell under mouse
	if input.mouseDown(2) then
		self.Map[gx..","..gy] = nil
	end
end

function Scene:Draw()
	graphics.setColor(255,255,255,255)

	local gw,gh = self.GridWidth,self.GridHeight
	local tw,th = self.TileWidth,self.TileHeight
	for y=0,self.MapSize.y do
		for x=0,self.MapSize.x do
		
			graphics.setColor(255,255,255,255)
			graphics.drawPart(self.sheet, x*gw,y*gh, gw,gh, 0*tw,0*th,tw,th)
		
			local cell = self.Map[x..","..y]
			
			if cell ~= nil then
				local item = ITEMS[cell.ID]
				local src = item.Source
				local color = item.c
				graphics.setColor(unpack(color))
				cell:Draw()
			end
			
		end
	end
end